/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package org.whuims.service.document;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.whuims.entity.Document;
import org.whuims.repository.jpa.DocumentDao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

// Spring Bean的标识.
@Component
// 类中所有public函数都纳入事务管理的标识.
@Transactional
public class DocumentService {

    private DocumentDao documentDao;

    public Document getDocument(Long id) {
        return documentDao.findOne(id);
    }

    public void save(Document entity) {
        documentDao.save(entity);
    }

    public void delete(Long id) {
        documentDao.delete(id);
    }

    public List<Document> getAllDocuments() {
        return (List<Document>) documentDao.findAll();
    }

    public List<Document> findTopDocuments(int size) {
        Sort sort = new Sort(Direction.DESC, "id");
        PageRequest pr = new PageRequest(0, size, sort);
        return documentDao.findAll(pr).getContent();
    }

    public Page<Document> getDocumentList(Map<String, Object> searchParams, int pageNumber, int pageSize,
                                          String sortType) {
        PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType);
        if (searchParams != null) {
            Specification<Document> spec = buildSpecification(searchParams);
            return documentDao.findAll(spec, pageRequest);
        }
        return documentDao.findAll(pageRequest);
    }

    public Page<Document> findAllByUserId(Long userId, int pageNumber, int pageSize,
                                          String sortType) {
        PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType);
        if (userId != null) {
            Specification<Document> spec = buildSpecification(userId);
            return documentDao.findAll(spec, pageRequest);
        }
        return documentDao.findAll(pageRequest);
    }

    /**
     * 创建分页请求.
     */
    private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
        Sort sort = null;
        if (sortType == null) sortType = "auto";
        if ("auto".equals(sortType)) {
            sort = new Sort(Direction.DESC, "id");
        } else if ("title".equals(sortType)) {
            sort = new Sort(Direction.ASC, "name");
        }
        return new PageRequest(pageNumber - 1, pagzSize, sort);
    }

    /**
     * 创建动态查询条件组合.
     */
    private Specification<Document> buildSpecification(Map<String, Object> searchParams) {
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<Document> spec = DynamicSpecifications.bySearchFilter(filters.values(), Document.class);
        return spec;
    }

    private Specification<Document> buildSpecification(Long userId) {
        Map<String, SearchFilter> filters = new HashMap<>();
        SearchFilter searchFilter = new SearchFilter("userId", SearchFilter.Operator.EQ, String.valueOf(userId));
        filters.put("userId", searchFilter);
        Specification<Document> spec = DynamicSpecifications.bySearchFilter(filters.values(), Document.class);
        return spec;
    }

    public List<Long> getAllId() {
        return documentDao.getAllId();
    }

    @Autowired
    public void setDocumentDao(DocumentDao documentDao) {
        this.documentDao = documentDao;
    }

}
